package com.rs.portal.reserve.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.jaxplat.commons.Row;
import com.rs.city.CityDelegate;
import com.rs.commons.RsConstant;
import com.rs.commons.web.PortalBaseAction;

public class WebCityAction extends PortalBaseAction
{
    public ModelAndView getCityList(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        CityDelegate delegate = new CityDelegate();

        List citis = delegate.getCityList();
        Map attrs = new HashMap();
        attrs.put("cityList", citis);
        return new ModelAndView("cityList", attrs);
    }

    public ModelAndView cities(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        CityDelegate delegate = new CityDelegate();

        List citis = delegate.getCityList();
        Map attrs = new HashMap();
        attrs.put("city_list", citis);
        return new ModelAndView("city", attrs);
    }

    public ModelAndView setCity(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        Row params = this.parseRequestParametersToRow(request);
        CityDelegate delegate = new CityDelegate();
        try
        {
            if (null != params.getString("code"))
            {
                String cityCode = params.getString("code");
                Row city = delegate.getCity(cityCode);
                if (null != city)
                {
                    request.getSession().setAttribute("curr_city_code", city.getString("city_code"));
                    request.getSession().setAttribute("curr_city_name", city.getString("city_name"));
                }
                else
                {
                    request.getSession().setAttribute("curr_city_code", RsConstant.DEFAULT_CITY_CODE);
                    request.getSession().setAttribute("curr_city_name", RsConstant.DEFAULT_CITY_NAME);
                }
            }
        }
        catch (Exception e)
        {
            // TODO: handle exception
        }

        return new ModelAndView("redirect:/web2/indexAction.do?method=index");
    }
}
